package com.gmss.crawler.service;

import com.alibaba.fastjson.JSON;
import com.gmss.crawler.module.paper.domain.Paper;
import com.gmss.crawler.module.paper.service.IPaperService;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.person.service.IPersonInfoService;
import com.gmss.crawler.utils.CommonUtils;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import com.gmss.crawler.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 百度学术爬取
 */
@Slf4j
@Service
public class BaiDuXueShuCrawlerService {

    private static final String baiDuXueShuCrawlerPath = "https://xueshu.baidu.com/s?wd=[]&pn=";

    @Resource
    private IPersonInfoService iPersonInfoService;
    @Resource
    private IPaperService iPaperService;

    public static void main(String[] args) throws Exception {
        Paper paper = new Paper();
        PersonInfo personInfo = new PersonInfo().builder().name("宋健").build();
        new BaiDuXueShuCrawlerService().savePaper(paper, personInfo);
    }


    public void savePaper(Paper paper, PersonInfo personInfo) throws Exception {
        String indexPathUrl = RegexUtils.replaceData(baiDuXueShuCrawlerPath, RegexUtils.ALL_ZHONG_REGEX, personInfo.getName());
        indexPathUrl = indexPathUrl + (0 * 10);
        System.out.println(indexPathUrl);
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/xueshu-" + personInfo.getName() + ".html");
//        Document doc = CrawlerCommonUtils.getDocument(indexPathUrl);
//        System.out.println(doc);
        Element element = doc.getElementById("bdxs_result_lists");
        // op-scholar-authorcard-wr
        Elements largeElements = element.select(".c-gap-left-large");
        List<Map<String, String>> list = new ArrayList<>();
        if (null != largeElements && largeElements.size() > 0) {
            String workCompany = largeElements.get(0).text();
            list = getPaperDetailData(personInfo, element, workCompany);
        } else {
            Elements authorcard = element.select(".op-scholar-authorcard-wr");
            Elements divEl = authorcard.get(0).select(".op-scholar-authorcard-wr").select("div");
            System.out.println(divEl.get(0).select("span").select("span").select("a").attr("href"));
            System.out.println(divEl.get(0).select("span").select("span").select("p").text());
        }
        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, String> map : list) {
                String paperTitle = map.get("paperTitle");
                String paperDetailUrl = map.get("paperUrl");
                Thread.sleep(CommonUtils.randomRange(2000, 5000));
                Document paperDetailDoc = CrawlerCommonUtils.getDocument(paperDetailUrl);
                Element dtlLeft = paperDetailDoc.getElementById("dtl_l");
                String authors = dtlLeft.select(".c_content .author_wr p span").text();
                authors = authors.replaceAll("：", "").replaceAll("展开", "");
                if (!StringUtils.contains(authors, personInfo.getName())) {
                    continue;
                }

                String abstracts = dtlLeft.select(".c_content .abstract_wr p").text();
                abstracts = abstracts.replaceAll("摘要", "").replaceAll("：", "").replaceAll("展开", "");
                abstracts = RegexUtils.replaceData(abstracts, RegexUtils.ALL_ZHONG_REGEX, "");
                String keyWords = dtlLeft.select(".c_content .kw_wr p span").text();
                keyWords = keyWords.replaceAll("关键词", "").replaceAll("：", "");

                String doi = dtlLeft.select(".c_content .doi_wr p").text();
                doi = doi.replaceAll("DOI", "").replaceAll("：", "");

                String ref = dtlLeft.select(".c_content .ref_wr p").text();
                ref = ref.replaceAll("被引量", "").replaceAll("：", "");

                String year = dtlLeft.select(".c_content .year_wr p").text();
                year = year.replaceAll("年份", "").replaceAll("：", "");

                Element dtlRight = paperDetailDoc.getElementById("dtl_r");
                String periodical = dtlRight.select(".dtl_journal .dtl_journal_container .container_right").select("a").text();

                String researchPoint = dtlRight.select(".dtl_search_word").select("div a").text();

                paper.setTitle(paperTitle);
                paper.setOtherAuthor(authors);
                paper.setDescribeData(abstracts);
                paper.setKeyword(keyWords);
                paper.setDoi(doi);
                paper.setPublicationDate(year);
                paper.setPeriodical(periodical);
                paper.setResearchPoint(researchPoint);
                iPaperService.saveOrUpdate(paper);
            }
        }

    }

    /**
     * 获取论文详情页面的URL,标题名称
     *
     * @param personInfo  人员bean
     * @param element     论文列表
     * @param workCompany 人员所在单位
     * @return 保存URL, 标题名称的List
     */
    private List<Map<String, String>> getPaperDetailData(PersonInfo personInfo, Element element, String workCompany) {
        if (StringUtils.isNotBlank(workCompany)) {
            personInfo.setWorkCompany(workCompany);
            // 保存、更新人员对应单位信息
//                iPersonInfoService.queryCompany(personInfo);
        }
        Elements aElements = element.select("div").select(".t").select(".c_font").select("h3").select("a");
        List<Map<String, String>> list = new ArrayList<>();
        if (null != aElements && aElements.size() > 0) {
            for (Element aEl : aElements) {
                Map<String, String> paperInfoMap = new HashMap<>();
                paperInfoMap.put("paperTitle", aEl.text());
                paperInfoMap.put("paperUrl", aEl.attr("href"));
                list.add(paperInfoMap);
            }
        }
        System.out.println(JSON.toJSONString(list));
        return list;
    }
}
